/*
 * @Author: chengsl
 * @Date: 2022-09-26 16:37:51
 * @LastEditors: chengsl
 * @LastEditTime: 2022-12-29 15:02:16
 * @Description: file content
 */
/**
 * @desc 函数防抖
 * @param func 函数
 * @param wait 延迟执行毫秒数
 * @param immediate true 表立即执行，false 表非立即执行
 */
function debounce(func, wait, immediate) {
  let timeout

  return function () {
    // let context = this
    // let args = arguments

    if (timeout) clearTimeout(timeout)
    if (immediate) {
      var callNow = !timeout
      timeout = setTimeout(() => {
        timeout = null
      }, wait)
      if (callNow) {
        // func.apply(context, args)
        func()
      }
    } else {
      timeout = setTimeout(function () {
        // func.apply(context, args)
        func()
      }, wait)
    }
  }
}

/**
 * 节流函数
 */
function throttle(yourFun, dealy) {
  let timer = null
  return function () {
    // const that = this
    // const args = arguments
    if (!timer) {
      timer = setTimeout(() => {
        // yourFun.apply(that, args)
        yourFun()
        timer = null
      }, dealy)
    }
  }
}

/**
 * 处理带脱敏字符串(中间替换*)
 * @param str 需要隐藏的字符串
 * @param frontLen 前面显示几位
 * @param endLen 末尾显示几位
 */
function filterHideStr (str, frontLen = 0, endLen = 0){
  if (!str || !str.length) {
    return str
  }
  const len = str.length - frontLen - endLen
  let xing = ''
  for (let i = 0; i < len; i++) {
    xing += '*'
  }
  return str.substring(0, frontLen) + xing + str.substring(str.length - endLen)
}

/**
 * copy内容到粘贴板
 * @param content 需要粘贴的内容
 */
function copyContent(content, tip) {
  var input = document.createElement('input')
  input.value = content
  document.body.appendChild(input)
  input.select()
  document.execCommand('copy')
  Vue.prototype.$message.success(tip || '复制成功!')
  document.body.removeChild(input)
}

/**
 * 添加原型属性 $getDialogHeight 计算弹框高度
 * @param {*} fatherClassName 父容器的样式class名
 * @param {*} loseHeight 额外减去的高度
 * @param {*} dontAddOverflowY 不自动加滚动条
 */
function getDialogHeight(fatherClassName, loseHeight, dontAddOverflowY) {
  const className = fatherClassName
    ? fatherClassName.includes('.') ? fatherClassName : ('.' + fatherClassName)
    : '.el-dialog'
  const dialogBody = document.querySelector(`${className} .el-dialog__body`)
  const headerHeight = document.querySelector(`${className} .el-dialog__header`)?.offsetHeight || 0
  if (!dontAddOverflowY) {
    dialogBody.style.overflowY = 'auto'
  }
  dialogBody.style.maxHeight = (document.body.clientHeight - headerHeight - loseHeight || 150) + 'px'
  console.log('计算dialogBody高度, className:', className, ', body:', document.body.clientHeight, '-headerHeight:', headerHeight, '-', (headerHeight + loseHeight || 120), '=', (document.body.clientHeight - headerHeight - loseHeight || 50))
}

/**
 * 复杂对象深拷贝
 * @param {*} obj
 * @returns
 */
function deepCopyObj(obj) {
  if (obj == null) {
    return null
  }
  var result = Array.isArray(obj) ? [] : {}
  for (let key in obj) {
    // eslint-disable-next-line no-prototype-builtins
    if (obj.hasOwnProperty(key)) {
      if (typeof obj[key] === 'object') {
        result[key] = deepCopyObj(obj[key]) // 如果是对象，再次调用该方法自身
      } else {
        result[key] = obj[key]
      }
    }
  }
  return result
}

export { debounce, throttle, filterHideStr, copyContent, getDialogHeight, deepCopyObj }

/**
 * @description 获取当前时间
 * @return string
 */
export function getTimeState() {
	// 获取当前时间
	let timeNow = new Date();
	// 获取当前小时
	let hours = timeNow.getHours();
	// 判断当前时间段
	if (hours >= 6 && hours <= 10) return `早上好 ⛅`;
	if (hours >= 10 && hours <= 14) return `中午好 🌞`;
	if (hours >= 14 && hours <= 18) return `下午好 🌞`;
	if (hours >= 18 && hours <= 24) return `晚上好 🌛`;
	if (hours >= 0 && hours <= 6) return `凌晨好 🌛`;
}
